package main;

import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;

/**
 * 判断用户的在线情况以及发送通知
 *
 * @author hanj.cn @outlook.com
 */
public class Judge {

    /**
     * 在线的设备列表
     */
    public static ArrayList<String> onlineDeviceList = new ArrayList<>();

    private static boolean isFirstTime = true;

    /**
     * 程序入口
     *
     * @param args the input arguments
     * @throws InterruptedException the interrupted exception
     */
    public static void main(String[] args) throws InterruptedException {

        handleData();

    }

    /**
     * 处理数据
     *
     * @throws InterruptedException the interrupted exception
     */
    public static void handleData() throws InterruptedException {

        String formattedTime;
        String tip;
        Date time;

        // 获取我的watchlist
        ArrayList<User> watchlist = Initialize.getUsers();

        while (true) {

            Timer timer = new Timer();

            // 开启定时器获取在线的设备列表
            timer.schedule(new TimerTask(), 0, Initialize.getQueryTimeInterval() * 1000L);

            // 设定连续运行时间为一分钟
            Thread.sleep(Initialize.getTotalDuration() * 60000L);

            // 关闭timer，释放资源
            timer.cancel();
            timer.purge();

            time = MyUtils.getTime();

            formattedTime = "[" + time + "]";

            tip = Initialize.getTotalDuration() + "分钟内的所有在线设备:\n" + onlineDeviceList.toString();

            System.out.println("\n" + formattedTime + "\n" + tip);

            if (isFirstTime) {
                MyUtils.writeToFile(Initialize.LOG_FILE_PATH, "\n" + formattedTime.substring(0, 11) + "]" + "\n", true);
                isFirstTime = false;
            }

            MyUtils.writeToFile(Initialize.LOG_FILE_PATH, "\n" + formattedTime + "\n" + tip + "\n", true);

            // 开始处理数据
            for (User user : watchlist) {

                if (onlineDeviceList.contains(user.getUsername() + "的" + user.getDeviceName())) {

                    // 判断上次的状态
                    if (!user.isOnline()) {
                        sendMsgAndSavaDate(user, true, time);
                    }
                } else if (user.isOnline()) {
                    sendMsgAndSavaDate(user, false, time);
                }
            }

            // 集合清零
            onlineDeviceList.clear();

        }
    }

    /**
     * Send msg and sava date.
     *
     * @param user     the user
     * @param isOnline the is online
     * @param time     the time
     */
    public static void sendMsgAndSavaDate(User user, boolean isOnline, Date time) {

        String msg = (isOnline ? "↑" : "↓") + "用户:" + user.getUsername() + ", 设备:" + user.getDeviceName() + ", 状态:" + (isOnline ? "在线" : "离线") + ", 已推送消息";

        // 写入文件
        MyUtils.writeToFile(Initialize.LOG_FILE_PATH, msg + "\n", true);

        // 发送通知
        if ("bark".equals(Initialize.getDeliveryMethod())) {

            SendMsg.toBark(user.getUsername() + "的wifi连接情况", user.getUsername(), (isOnline ? "🟢" : "🔴") + user.getUsername() + "的" + user.getDeviceName() + "在" + Initialize.getTotalDuration() + "分钟内" + (isOnline ? "上线" : "离线") + "\n本次:" + time + "\n上次:" + (user.getTime() == null ? "由于初始化未能获取" : user.getTime().toString()));

        } else if ("推送熊".equals(Initialize.getDeliveryMethod())) {
            SendMsg.toPushBear(Initialize.getUUID(), user.getUsername() + "的" + user.getDeviceName() + "已" + (isOnline ? "上线" : "下线"));

        }
        // 存储时间
        user.setTime(time);

        //改变状态
        user.setOnline(isOnline);

        // 打印信息
        System.out.println(msg);
    }

}
